/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bsk;

import com.tiemens.secretshare.engine.SecretShare;
import com.tiemens.secretshare.engine.SecretShare.PublicInfo;
import com.tiemens.secretshare.engine.SecretShare.ShareInfo;
import com.tiemens.secretshare.engine.SecretShare.SplitSecretOutput;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Win7
 */
public class Shamir {

   private PublicInfo pubInfo;   
    
    public Shamir() {
      pubInfo=   new PublicInfo(5, 3, SecretShare.getPrimeUsedFor4096bigSecretPayload(),
                "secret");    
     }
    
    public BigInteger[] split(BigInteger key)
    {
       SecretShare share = new SecretShare(pubInfo);
       SplitSecretOutput split = share.split(key);
       
       BigInteger[] pieces= new BigInteger[split.getShareInfos().size()];
        int i=0;
        for (ShareInfo e : split.getShareInfos()) {
            pieces[i++]=e.getShare();
        }
        return pieces;
    }
    
    public BigInteger getPrivate(BigInteger[] pieces)
    { 
       SecretShare share = new SecretShare(pubInfo);
         
        ArrayList<ShareInfo> shareList=new ArrayList<>();
        
        for (int w=0;w<pieces.length; w++)
        shareList.add(new ShareInfo(w+1, pieces[w],pubInfo));
       
     return share.combine(shareList).getSecret();   
    }
}

